package com.zhiyan.common.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.zhiyan.security.account.domain.entity.Account;

/**
 * 知言 session 信息操作类
 * Created by panhs on 2017/5/14
 */
public class SecurityContextHolder {

    public static String USER_CONTEXT = "USER_CONTEXT";

    //获取当前用户
    public static Account getUser(){
    	HttpServletRequest request = ServletContextHolder.getRequest();
        return (Account)request.getSession().getAttribute(USER_CONTEXT);
    }

    //将用户信息设置到当前会话
    public static void setUser(Account user){
        HttpServletRequest request = ServletContextHolder.getRequest();
        HttpSession session = request.getSession();
        session.setAttribute(USER_CONTEXT, user);
    }

    //销毁当前会话
    public static void destroy(){
        HttpServletRequest request = ServletContextHolder.getRequest();
        HttpSession session = request.getSession();
        session.removeAttribute(USER_CONTEXT);
        session.invalidate();
    }
}
